package com.goldsprite.microhorigame.screens.tests.lightsystem;

import com.badlogic.gdx.*;
import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.graphics.g2d.*;
import com.badlogic.gdx.graphics.glutils.*;
import com.badlogic.gdx.math.*;
import com.badlogic.gdx.utils.*;
import com.badlogic.gdx.utils.viewport.*;
import com.goldsprite.gameframeworks.assets.GameRes;
import com.goldsprite.gameframeworks.shaders.*;

public class HackLightEngine {
	private final Array<HackLight> lights = new Array<>();
	protected final Color ambientLightColor;
	private final SpriteBatch batch;

	// FBOs
	private FrameBuffer lightsBuffer;
	private FrameBuffer ambientBuffer;
	private FrameBuffer sceneBuffer;
	private final TextureRegion lightsBufferRegion;
	private final TextureRegion ambientBufferRegion;
	private final TextureRegion sceneBufferRegion;

	// Shader
	private final LightBlendShader blendShader;

	private final Color transparentColor = new Color().mul(0);

	public HackLightEngine(float ambientR, float ambientG, float ambientB, float ambientA) {
		this.ambientLightColor = new Color(ambientR, ambientG, ambientB, ambientA);

		lightsBufferRegion = new TextureRegion();
		ambientBufferRegion = new TextureRegion();
		sceneBufferRegion = new TextureRegion();

		batch = new SpriteBatch();
		blendShader = new LightBlendShader();
	}

	public void createFBO(int width, int height) {
		// 点光源FBO
		if (lightsBuffer != null) lightsBuffer.dispose();
		lightsBuffer = new FrameBuffer(Pixmap.Format.RGBA8888, width, height, false);
		lightsBuffer.getColorBufferTexture().setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);
		lightsBufferRegion.setRegion(lightsBuffer.getColorBufferTexture());
		lightsBufferRegion.flip(false, true);

		// 环境光FBO
		if (ambientBuffer != null) ambientBuffer.dispose();
		ambientBuffer = new FrameBuffer(Pixmap.Format.RGBA8888, width, height, false);
		ambientBuffer.getColorBufferTexture().setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);
		ambientBufferRegion.setRegion(ambientBuffer.getColorBufferTexture());
		ambientBufferRegion.flip(false, true);

		// 场景FBO
		if (sceneBuffer != null) sceneBuffer.dispose();
		sceneBuffer = new FrameBuffer(Pixmap.Format.RGBA8888, width, height, false);
		sceneBuffer.getColorBufferTexture().setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);
		sceneBufferRegion.setRegion(sceneBuffer.getColorBufferTexture());
		sceneBufferRegion.flip(false, true);
	}

	/**
	 * 开始捕获场景（在渲染场景前调用）
	 */
	public void beginSceneCapture() {
		ScreenUtils.clear(0, 0, 0, 0);//清理之前的绘制
		sceneBuffer.begin();
		Gdx.gl.glViewport(0, 0, sceneBuffer.getWidth(), sceneBuffer.getHeight());
		ScreenUtils.clear(0, 0, 0, 0);
	}

	/**
	 * 结束场景捕获（在渲染场景后调用）
	 */
	public void endSceneCapture() {
		sceneBuffer.end();
	}

	Color tmpColor = new Color();
	public void draw(Matrix4 projectionMatrix, Viewport viewport, Color clearColor) {
		// 第一步：渲染环境光到ambientBuffer
		ambientBuffer.begin();
		Gdx.gl.glViewport(0, 0, ambientBuffer.getWidth(), ambientBuffer.getHeight());
//        ambientLightColor.set(Color.WHITE);
		ScreenUtils.clear(ambientLightColor);
		ambientBuffer.end();

		// 第二步：渲染点光源到lightsBuffer
		lightsBuffer.begin();
		Gdx.gl.glViewport(0, 0, lightsBuffer.getWidth(), lightsBuffer.getHeight());
		ScreenUtils.clear(transparentColor);

		Gdx.gl.glEnable(GL20.GL_BLEND);
		Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE);

		batch.begin();
		batch.setProjectionMatrix(projectionMatrix);
		batch.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE);

		for (HackLight light : lights) {
			light.draw(batch);
		}

		batch.end();
		lightsBuffer.end();

		// 恢复游戏视口
		viewport.apply();
		ScreenUtils.clear(clearColor);

		// 第三步：使用Shader混合三个纹理
		batch.begin();
		batch.setBlendFunctionSeparate(
			GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA,  // RGB混合
			GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA   // Alpha混合
		);
//		batch.setBlendFunctionSeparate(
//			GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA,  // RGB混合
//			GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA   // Alpha混合
//		);
//		batch.setBlendFunctionSeparate(
//			GL20.GL_ONE, GL20.GL_ONE_MINUS_SRC_ALPHA,  // RGB混合
//			GL20.GL_ONE, GL20.GL_ONE_MINUS_SRC_ALPHA   // Alpha混合
//		);
//		// 使用预乘Alpha的覆盖混合
//		batch.setBlendFunction(GL20.GL_ONE, GL20.GL_ONE_MINUS_SRC_ALPHA);
//		batch.enableBlending();
//		batch.setBlendFunctionSeparate(
//			GL20.GL_SRC_COLOR, GL20.GL_DST_COLOR,
//			GL20.GL_SRC_ALPHA, GL20.GL_DST_ALPHA
//		);
		batch.setProjectionMatrix(viewport.getCamera().combined);

		// 绑定纹理到不同单元
		lightsBuffer.getColorBufferTexture().bind(2);
		ambientBuffer.getColorBufferTexture().bind(1);
		sceneBuffer.getColorBufferTexture().bind(0);

		// 设置光源强度并开始Shader
		ShaderProgram.pedantic = false;
		blendShader.begin(batch);

		////绘制背景
		//batch.draw(gameBackRegion, 0, viewport.getWorldHeight(), viewport.getWorldWidth(), -viewport.getWorldHeight());

		// 绘制全屏矩形
		batch.draw(sceneBufferRegion, 0, 0, viewport.getWorldWidth(), viewport.getWorldHeight());

		blendShader.end(batch);
		batch.end();
	}

	public void dispose() {
		if (lightsBuffer != null) lightsBuffer.dispose();
		if (ambientBuffer != null) ambientBuffer.dispose();
		if (sceneBuffer != null) sceneBuffer.dispose();
		if (blendShader != null) blendShader.dispose();
		batch.dispose();
	}

	// 其他方法保持不变...
	public void addLight(HackLight light) {
		lights.add(light);
	}

	public Color getAmbientLightColor() {
		return ambientLightColor;
	}

	public void setAmbientLightColor(Color color) {
		ambientLightColor.set(color);
	}

	public void setAmbientLightColor(float r, float g, float b, float a) {
		ambientLightColor.set(r, g, b, a);
	}

	public int size() {
		return lights.size;
	}

	public HackLight getLight(int index) {
		return lights.get(index);
	}

	public void removeLight(int index) {
		lights.removeIndex(index);
	}

	public void removeLight(HackLight light) {
		lights.removeValue(light, true);
	}
}
